package com.vector.ding.web.service.bizinfo;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.vector.ding.commons.BizConstant;
import com.vector.ding.commons.util.StringUtil;
import com.vector.ding.commons.util.collection.CollectionUtil;
import com.vector.ding.commons.util.file.ExcelUtil;
import com.vector.ding.dao.mybatis.mapper.HjxxpfInfoMapper;
import com.vector.ding.dao.mybatis.model.HjxxpfInfo;
import com.vector.ding.dao.mybatis.model.HjxxpfInfoExample;
import com.vector.ding.exception.BaseException;

@Service
public class HjxxnrBizInfoMgrService {
	
	@Autowired
	private HjxxpfInfoMapper bizMapper;
	
	public List<HjxxpfInfo> getBizInfo(int pageIndex, int pageSize, 
			HttpServletRequest request) {
		PageHelper.startPage(pageIndex, pageSize, true);
		HjxxpfInfoExample example = parseBizInfoExample(request);
		//example.setOrderByClause("number asc");
		return bizMapper.selectByExample(example);
	}
	
	public HjxxpfInfo getBizInfo(String companyName) { 
		if (StringUtil.isEmpty(companyName))
			return null;
		
		HjxxpfInfoExample example = new HjxxpfInfoExample();
		   example.or().andCompanyNameEqualTo(companyName);
		List<HjxxpfInfo> list = bizMapper.selectByExample(example);
		return CollectionUtil.isEmpty(list) ? null : list.get(0);
	}
	
	private HjxxpfInfoExample parseBizInfoExample(HttpServletRequest request) {
		HjxxpfInfoExample example = new HjxxpfInfoExample();
		String companyName = StringUtil.getString(request.getParameter("company_name"));
		//if (StringUtil.isNotEmpty(companyName))
		example.or().andCompanyNameLike(StringUtil.wrapString(companyName, "%", "%"));
		return example;
	}
	
	public boolean addBizInfo(HjxxpfInfo record) {
		if (StringUtil.isEmpty(record.getNumber()))
			record.setNumber(UUID.randomUUID().toString());
		return 1 == bizMapper.insertSelective(record);
	}
	
	public boolean updateBizInfo(HjxxpfInfo record) {
		if (StringUtil.isEmpty(record.getNumber()))
			return false;
		return 1 == bizMapper.updateByPrimaryKeySelective(record);
	}
	
	public void deleteBizInfo(String... nums) {
		if (nums == null)
			return ;
		
		for (String num: nums) {
			bizMapper.deleteByPrimaryKey(num);
		}
	}
	
	public HjxxpfInfo parseBizInfo(HttpServletRequest request) {
		HjxxpfInfo record = new HjxxpfInfo();
		record.setBelongYear(new Integer(request.getParameter("belongYear")));
		record.setCompanyName(request.getParameter("companyName"));
		record.setBearName(request.getParameter("bearName"));
		record.setPrizeUnit(request.getParameter("prizeUnit"));
		
		String bearDate = request.getParameter("bearDate");
		String bearGrade = request.getParameter("bearGrade");
		if(StringUtil.isNotEmpty(bearDate))
			record.setBearDate(bearDate);
		if(StringUtil.isNotEmpty(bearGrade))
			record.setBearGrade(new BigDecimal(bearGrade));
		if (StringUtil.isEmpty(request.getParameter("number")))
			record.setNumber(UUID.randomUUID().toString());
		else
			record.setNumber(request.getParameter("number"));
		
		return record;
	}
	
	public void importFormFile(File importFile) {
		HSSFWorkbook workBook = null;
		int rowIndex = 1;
		try {
			workBook = new HSSFWorkbook(FileUtils.openInputStream(importFile));
			HSSFSheet sheet = workBook.getSheetAt(1);
			HSSFRow titleRow = sheet.getRow(4);
			for (int cellIdx = titleRow.getFirstCellNum(); cellIdx < titleRow.getLastCellNum(); ++cellIdx) {
				System.out.println(titleRow.getCell(cellIdx).toString());
			}
			int rowNum = sheet.getLastRowNum();
			for (rowIndex = 4; rowIndex <= rowNum; ++rowIndex) {
				HSSFRow row = sheet.getRow(rowIndex);
				boolean needImport = checkRowData(row);
				if (!needImport)
					continue;
				
				HjxxpfInfo bean = getBizInfoBean(row);
				bizMapper.insertSelective(bean);
			}
		} catch (Exception e) {
			e.printStackTrace();
//			throw new BaseException(e);
			throw new BaseException(String.format("第%s行数据格式有误，请检查数据！", rowIndex));
		} finally {
			if (workBook != null) {
				try {
					workBook.close();
				} catch (IOException e) {
					e.printStackTrace();
					throw new BaseException(e);
				}
			}
		}
	}
	
	public HjxxpfInfo getBizInfoBean(HSSFRow row) {
		HjxxpfInfo bean = new HjxxpfInfo();
		bean.setNumber(UUID.randomUUID().toString());
		bean.setCompanyName(ExcelUtil.getStringValue(row.getCell(BizConstant.Hjxx.Biz.COMPANY_NAME_CELL),false));
		bean.setBelongYear(ExcelUtil.getIntegerValue(row.getCell(BizConstant.Hjxx.Biz.BELONG_YEAR_CELL)));
		bean.setBearName(ExcelUtil.getStringValue(row.getCell(BizConstant.Hjxx.Biz.BEAR_NAME_CELL),true));
		bean.setPrizeUnit(ExcelUtil.getStringValue(row.getCell(BizConstant.Hjxx.Biz.PRIZE_UNIT_CELL),true));
		bean.setBearDate(ExcelUtil.getDateString(row.getCell(BizConstant.Hjxx.Biz.BEAR_DATE_CELL)));
		bean.setBearGrade(new BigDecimal(row.getCell(BizConstant.Hjxx.Biz.BEAR_GRADE_CELL).getNumericCellValue()));
		return bean;
	}
	
	private boolean checkRowData(HSSFRow row) {
		String companyName = row.getCell(BizConstant.Hjxx.Biz.COMPANY_NAME_CELL).toString();
		if (StringUtil.isEmpty(companyName))
			return false;
		
		return true;
//		HjxxpfInfoExample example = new HjxxpfInfoExample();
//		example.or().andNumberEqualTo(companyName);
//		return bizMapper.countByExample(example) == 0;
	}

}
